﻿using HyEduOnline.Entities;
using HyEduOnline.Server.Admin;
using HyEduOnline.Server.Student;
using HyEduOnline.Server.Teacher;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;

namespace HyEduOnline.Models
{
    public class LoginValidate
    {
        private const int StudentType = 0;
        private const int TeacherType = 1;
        private const int MangerType = 2;
        private const int AllType = 3;

        #region 教师登陆验证
        // <summary>
        /// 教师登录验证
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="PassWord"></param>
        /// <returns></returns>
        public static bool ValidateTeacher(string UserName, string PassWord)
        {
            HttpSessionState Session = System.Web.HttpContext.Current.Session;

            string WhereClause = "S_TeaNo ='" + UserName + "' and  S_PassWord='" + PassWord + "' ";
            TeachersDAO teaDao = new TeachersDAO();
            List<TbTeachers> list = teaDao.GetByPage(WhereClause, 0, int.MaxValue).ToList();
            if (list.Count != 0)
            {

                TbTeachers teaItem = list[0];//取出找到的用户
                //更新最后登录时间
                teaItem.DLastDate = DateTime.Now;
                teaDao.Update(teaItem);
                //将相关信息放入Session中
                Session["TeacherID"] = teaItem.Id;
                Session["TeaNo"] = teaItem.STeaNo;
                //test
                //FrontUserInfo.SetCurrentUserName(teaItem.STeaName);//获取当前用户姓名
                //FrontUserInfo.SetCurrentUserNo(teaItem.STeaNo);//获取当前用户编号
                Session["TeaName"] = teaItem.STeaName;
                Session["UserType"] = TeacherType;//设置教师身份
                if (!string.IsNullOrEmpty(teaItem.SPicUrl))
                {
                    string picUrl = teaItem.SPicUrl.Replace("/large/", "/small/");//头像大小替换
                    Session["Image"] = picUrl;
                }
                Session["TeaPartCode"] = teaItem.SColleCode;//学院、部门代码

                //找到当前登录教师所有教授的课程
                TeaOpenCourseDAO teaOpenCourse = new TeaOpenCourseDAO();
                IList<TbTeaOpenCourse> teaCourse = teaOpenCourse.findBySTeacherCode(teaItem.STeaNo);

                IList<TbFrontMenus> spLists = new FrontMenusDAO().GetByPage("I_UserType=1", 0, int.MaxValue);
                if (spLists.Count > 0)
                {
                    //接下来进行节点组装

                    HyEduOnline.Server.Common.ComTreeNode root = new HyEduOnline.Server.Common.ComTreeNode();
                    root.NodeName = "根结点";
                    Dictionary<int, HyEduOnline.Server.Common.ComTreeNode> nodeslist = new Dictionary<int, HyEduOnline.Server.Common.ComTreeNode>();
                    foreach (var itemPower in spLists)
                    {
                        HyEduOnline.Server.Common.ComTreeNode myNode = new HyEduOnline.Server.Common.ComTreeNode();
                        HyEduOnline.Server.Common.ComTreeNode tempNode;
                        myNode.NodeName = itemPower.SMenuName;

                        myNode.Id = itemPower.Id;
                        myNode.Url = itemPower.SMenuUrl;
                        myNode.Tip = itemPower.SPicurl;

                        nodeslist.Add(itemPower.Id, myNode);

                        //将该教师授课课程加入到菜单中
                        if (itemPower.Id == 1)
                        {
                            tempNode = (HyEduOnline.Server.Common.ComTreeNode)nodeslist[(int)itemPower.Id];

                            foreach (var item in teaCourse)
                            {
                                HyEduOnline.Server.Common.ComTreeNode myCourse = new HyEduOnline.Server.Common.ComTreeNode();

                                myCourse.NodeName = item.SCourseName;
                                myCourse.Id = item.Id;
                                myCourse.Url = "/Teacher/OutlineShow?CourseId=" + (int)item.ICourseId;
                                myCourse.Tip = "fa fa-fw fa-book";
                                tempNode.Childrens.Add(myCourse);
                            }
                        }


                        if (Convert.ToInt32(itemPower.IParentId) != 0)
                        {
                            tempNode = (HyEduOnline.Server.Common.ComTreeNode)nodeslist[(int)itemPower.IParentId];

                            tempNode.Childrens.Add(myNode);
                        }
                        else
                        {
                            root.Childrens.Add(myNode);
                        }

                    }

                    Session["SysMenus"] = root;
                    return true;
                }
            }
            return false;
        }  
        #endregion

        #region 学生登录验证
        /// <summary>
        /// 学生登录验证
        /// </summary>
        /// <returns></returns>
        public static bool ValidateStudent(string UserName, string PassWord)
        {
            HttpSessionState Session = System.Web.HttpContext.Current.Session;

           


            string WhereClause = "S_No ='" + UserName + "' and  S_PassWord='" + PassWord + "' ";
            StudentsDAO studentsDAO = new StudentsDAO();
            List<TbStudents> list = studentsDAO.GetByPage(WhereClause, 0, int.MaxValue).ToList();
            if (list.Count != 0)
            {
                TbStudents student = list[0];//取出找到的用户
                //更新最后登录时间
                student.DLastDate = DateTime.Now;
                studentsDAO.Update(student);
                Session["StuName"] = student.SName;
                Session["StudentID"] = student.Id;
                Session["StuNo"] = student.SNo;
                //test11.13
                //FrontUserInfo.SetCurrentUserName(student.SName);//获取当前用户姓名
               // FrontUserInfo.SetCurrentUserNo(student.SNo);//获取当前用户编号
                Session["UserType"] = StudentType;
                if (!string.IsNullOrEmpty(student.SPicUrl))
                {
                    string picUrl = student.SPicUrl.Replace("/large/", "/small/");
                    Session["Image"] = picUrl;
                }
                Session["StuProCode"] = student.SProCode;

                //找到当前登录学生所教授的课程
                DeanStuSelCourseDAO stuCourse = new DeanStuSelCourseDAO();
                IList<TbDeanStuSelCourse> Course = stuCourse.findBySStuNo(student.SNo);

                IList<TbFrontMenus> spLists = new FrontMenusDAO().GetByPage("I_UserType=0", 0, int.MaxValue);
                if (spLists.Count > 0)
                {
                    //接下来进行节点组装

                    HyEduOnline.Server.Common.ComTreeNode root = new HyEduOnline.Server.Common.ComTreeNode();
                    root.NodeName = "根结点";
                    Dictionary<int, HyEduOnline.Server.Common.ComTreeNode> nodeslist = new Dictionary<int, HyEduOnline.Server.Common.ComTreeNode>();
                    foreach (var itemPower in spLists)
                    {
                        HyEduOnline.Server.Common.ComTreeNode myNode = new HyEduOnline.Server.Common.ComTreeNode();
                        HyEduOnline.Server.Common.ComTreeNode tempNode;
                        myNode.NodeName = itemPower.SMenuName;

                        myNode.Id = itemPower.Id;
                        myNode.Url = itemPower.SMenuUrl;
                        myNode.Tip = itemPower.SPicurl;

                        nodeslist.Add(itemPower.Id, myNode);

                        //将该学生所学课程加入到菜单中
                        if (itemPower.Id == 7)
                        {
                            tempNode = (HyEduOnline.Server.Common.ComTreeNode)nodeslist[(int)itemPower.Id];

                            foreach (var item in Course)
                            {
                                HyEduOnline.Server.Common.ComTreeNode myCourse = new HyEduOnline.Server.Common.ComTreeNode();

                                myCourse.NodeName = item.SCourseName;
                                myCourse.Id = (int)item.Id;
                                myCourse.Url = "/Student/OutlineShow?CourseId=" + (int)item.ICourseId + "&TeaID=" + item.ITeacherId;
                                myCourse.Tip = "fa fa-fw fa-book";
                                tempNode.Childrens.Add(myCourse);
                            }
                        }


                        if (Convert.ToInt32(itemPower.IParentId) != 0)
                        {
                            tempNode = (HyEduOnline.Server.Common.ComTreeNode)nodeslist[(int)itemPower.IParentId];

                            tempNode.Childrens.Add(myNode);
                        }
                        else
                        {
                            root.Childrens.Add(myNode);
                        }

                    }

                    Session["SysMenus"] = root;
                    return true;
                }
            }
            return false;
        } 
        #endregion
    }
}